let logger = require("pomelo-logger").getLogger(__filename);
let niuniuGameResultLoader = require("../entities/gameResult/niuniuGameResult").loadFromDocument;
let niuniuGameResultCreator = require("../entities/gameResult/niuniuGameResult").create;
let zhajinhuaGameResultLoader = require("../entities/gameResult/zhajinhuaGameResult").loadFromDocument;
let zhajinhuaGameResultCreator = require("../entities/gameResult/zhajinhuaGameResult").create;
let DateUtil = require("../../utils/dateUtil");

const NIUNIU_GID = 10001;
const ZHAJINHUA_GID = 10002;


let GameResultService = function (app) {
    this.app = app;
};

GameResultService.prototype.getNiuniuGameResult = function (uid, callback) {
    let gameResultDao = this.app.get("gameResultDao");
    gameResultDao.findByPlayerId(NIUNIU_GID, uid, function (documents) {
        if(!documents) {
            callback([]);
            return;
        }
        let gameResults = [];
        for (let document of documents) {
            gameResults.push(niuniuGameResultLoader(document));
        }
        callback(gameResults);
    });
};

GameResultService.prototype.getZhajinhuaGameResult = function (uid, callback) {
    let gameResultDao = this.app.get("gameResultDao");
    gameResultDao.findByPlayerId(ZHAJINHUA_GID, uid, function (documents) {
        if(!documents) {
            callback([]);
            return;
        }
        let gameResults = [];
        for (let document of documents) {
            gameResults.push(zhajinhuaGameResultLoader(document));
        }
        callback(gameResults);
    });
};

GameResultService.prototype.addNiuniuGameResult = function (msg, callback) {
    let gameResultDao = this.app.get("gameResultDao");
    let niuniuGameResult = niuniuGameResultCreator(msg.gid, msg.tid, msg.code, msg.multiplier, msg.startTime,
                                                    msg.endTime, msg.dealerType, msg.multiplierType,
                                                    msg.maxAuctionDealerMultiplier, msg.results);
    gameResultDao.addGameResult(niuniuGameResult.toBSON(), result =>{
        if(!result || result.result.ok != 1) {
            logger.info(`addNiuniuGameResult error. result empty`);
            callback(null);
            return;
        }
        callback(niuniuGameResult);
    });
};


GameResultService.prototype.addZhajinhuaGameResult = function (msg, callback) {
    let gameResultDao = this.app.get("gameResultDao");
    let zhajinhuaGameResult = zhajinhuaGameResultCreator(msg.gid, msg.tid, msg.code, msg.multiplier, msg.startTime,
                                                            msg.endTime, msg.maxRounds, msg.minSeeCardRound,
                                                            msg.minCompareRound, msg.results);
    gameResultDao.addGameResult(zhajinhuaGameResult.toBSON(), result =>{
        if(!result || result.result.ok != 1) {
            logger.info(`addZhajinhuaGameResult error. result empty`);
            callback(null);
            return;
        }
        callback(zhajinhuaGameResult);
    });
};

GameResultService.prototype.serializeGameResults = function (results) {
    let serializeResults = [];
    for(let result of results) {
        serializeResults.push(result.toJSON());
    }
    return serializeResults;
};

GameResultService.prototype.serializeGameRecords = function (gameRecords) {
    let serializeGameRecords = [];
    for(let gameRecord of gameRecords) {
        serializeGameRecords.push(gameRecord.toJSON());
    }
    return serializeGameRecords;
};

module.exports.create = function (app) {
    return new GameResultService(app);
};
